React Native 常见的一些问题

启动项目常见错误

unable to load script from asset/index.android.bundle

在 \android\app\src\main 目录下新建一个 asset 文件夹,并在 terminal 种运行下面的命令

1
react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res

再次执行

1
react-native run-android

** 注 ** :在 0.49 版本后, react-native 项目中 index.android.js 和 index.ios.js 统一合并成了 index.js,所以0.49 版本以后使用上述命令需要将入口文件改为 index.js。

Could not install the app on the device

  1. :app:processDebugResources
    1
    cd android && gradlew clean
  2. Could not find tools.jar.Please check that … contains a valid JDk
    jdk1.8 安装不正确或环境变量配置错误
  3. JAVA_HOME is set to an invalid directory: C:\Program Files\Java\jdk1.8.0_201;
    环境变量配置去掉分号
  4. :app:installDebug
    手机未开启调试模式或未启用模拟器

开发常用功能

Live Reload

模拟器 Ctrl + m 开启 Live reload
真机摇晃设备

remote js debugging

模拟器打开 dev setting -> debug server host & port for device 设置 ip:8081
chrome 打开 http://localhost:8081/debugger-ui/

commend run simulator

Android studio 中可以直接启用模拟器,如果不希望启动 Android studio,可以从命令行运行模拟器
进入到 SDK 安装目录中执行下面命令

1
2
//查看模拟器列表
emulator.exe -list-avds
1
2
//启动模拟器
emulator.exe -netdelay none -netspeed full -avd 设备名称

除了 Android studio 安装的模拟器之外,也可以使用其它平台的模拟器进行调试,如 Genymotion 等模拟器。

参考

React Native 官方文档

作者

晓策

发布于

2019-02-19

更新于

2023-05-25

许可协议

评论